#include <regx52.h>
int time=0;
int time_seed;
unsigned int i;
/**
 * 实现一个按键，每次点击按键随机点亮0~7位置的其中一个灯
 */
void timer0_init()
{
    TMOD &= 0xF0;
    TMOD |= 0x01;

    TH0 = 0xDB;

    TL0 = 0xFF;

    TR0 = 1;

    ET0 = 1;

    EA = 1;
    
    PT0 = 1;
}

unsigned int timer_rand()
{
    time_seed = (time * 123 + 8) % 8;

    return time_seed;
}

void timer0_ISR() interrupt 1
{
    
  TH0 = 0xDB;
    
  TL0 = 0xFF;

  time=time+1;

}
void receive(unsigned int i)
{
    if(i==0)
    {
     P2=~0x01;
    }
    if(i==1)
    {
     P2=~0x02;
    }
    if(i==2)
    {
     P2=~0x04;
    }
    if(i==3)
    {
     P2=~0x08;
    }
    if(i==4)
    {
     P2=~0x10;
    }
    if(i==5)
    {
     P2=~0x20;
    }
    if(i==6)
    {
     P2=~0x40;
    }
    if(i==7)
    {
     P2=~0x80;
    }
}
void main()
{
    timer0_init();
    while(1)
    {
      if(P1_0==0)
      {
       while(P1_0==0){}
       i=timer_rand();
       receive(i);
      }

    }
}
